import 'package:app_30/json/detail_entity_helper.dart';
import 'package:app_30/json/gogngao_entity_helper.dart';
import 'package:app_30/json/referral_entity_helper.dart';
import 'package:app_30/models/detail_entity.dart';
import 'package:app_30/models/gogngao_entity.dart';
import 'package:app_30/models/img_entity.dart';
import 'package:app_30/models/ip_entity.dart';
import 'package:app_30/models/load_entity.dart';
import 'package:app_30/models/referral_entity.dart';
import 'package:app_30/models/regist_num_entity.dart';
import 'package:app_30/models/tgjf_entity.dart';
import 'package:app_30/models/vip_entity.dart';

import '../img_entity_helper.dart';
import '../ip_entity_helper.dart';
import '../load_entity_helper.dart';
import '../regist_num_entity_helper.dart';
import '../tgjf_entity_helper.dart';
import '../vip_entity_helper.dart';

class JsonConvert<T> {
  T fromJson(Map<String, dynamic> json) {
    return _getFromJson<T>(runtimeType, this, json);
  }

  static _getFromJson<T>(Type type, data, json) {
    switch (type) {
      case IpEntity:
        return ipEntityFromJson(data as IpEntity, json) as T;
      case LoadEntity:
        return loadEntityFromJson(data as LoadEntity, json) as T;
      case DetailEntity:
        return detailEntityFromJson(data as DetailEntity, json) as T;
      case GonggaoEntity:
        return gonggaoEntityFromJson(data as GonggaoEntity, json) as T;
      case ImgEntity:
        return imgEntityFromJson(data as ImgEntity, json) as T;
      case ReferralEntity:
        return referralEntityFromJson(data as ReferralEntity, json) as T;
      case RegistNumEntity:
        return registNumEntityFromJson(data as RegistNumEntity, json) as T;
      case TgjfEntity:
        return tgjfEntityFromJson(data as TgjfEntity, json) as T;
      case VipEntity:
        return vipEntityFromJson(data as VipEntity, json) as T;
    }
    return data as T;
  }

  // Map<String, dynamic> toJson() {
  //   return _getToJson<T>(runtimeType, this);
  // }

  // static _getToJson<T>(Type type, data) {
  //   switch (type) {
  //     case IpEntity:
  //       return ipEntityToJson(data as IpEntity);
  //   }
  //   return data as T;
  // }

  //Go back to a single instance by type
  static _fromJsonSingle<M>(json) {
    String type = M.toString();
    if (type == (IpEntity).toString()) {
      return IpEntity().fromJson(json);
    }

    if (type == (LoadEntity).toString()) {
      return LoadEntity().fromJson(json);
    }
    if (type == (DetailEntity).toString()) {
      return DetailEntity().fromJson(json);
    }
    if (type == (GonggaoEntity).toString()) {
      return GonggaoEntity().fromJson(json);
    }
    if (type == (ImgEntity).toString()) {
      return ImgEntity().fromJson(json);
    }
    if (type == (ReferralEntity).toString()) {
      return ReferralEntity().fromJson(json);
    }
    if (type == (RegistNumEntity).toString()) {
      return RegistNumEntity().fromJson(json);
    }
    if (type == (TgjfEntity).toString()) {
      return TgjfEntity().fromJson(json);
    }
    if (type == (VipEntity).toString()) {
      return VipEntity().fromJson(json);
    }
    return null;
  }

  //list is returned by type
  static M _getListChildType<M>(List data) {
    if (<IpEntity>[] is M) {
      return data.map<IpEntity>((e) => IpEntity().fromJson(e)).toList() as M;
    }

    if (<LoadEntity>[] is M) {
      return data.map<LoadEntity>((e) => LoadEntity().fromJson(e)).toList()
          as M;
    }
    if (<DetailEntity>[] is M) {
      return data.map<DetailEntity>((e) => DetailEntity().fromJson(e)).toList()
          as M;
    }
    if (<GonggaoEntity>[] is M) {
      return data
          .map<GonggaoEntity>((e) => GonggaoEntity().fromJson(e))
          .toList() as M;
    }
    if (<ImgEntity>[] is M) {
      return data.map<ImgEntity>((e) => ImgEntity().fromJson(e)).toList() as M;
    }
    if (<ReferralEntity>[] is M) {
      return data
          .map<ReferralEntity>((e) => ReferralEntity().fromJson(e))
          .toList() as M;
    }

    if (<RegistNumEntity>[] is M) {
      return data
          .map<RegistNumEntity>((e) => RegistNumEntity().fromJson(e))
          .toList() as M;
    }
    if (<TgjfEntity>[] is M) {
      return data.map<TgjfEntity>((e) => TgjfEntity().fromJson(e)).toList()
          as M;
    }
    if (<VipEntity>[] is M) {
      return data.map<VipEntity>((e) => VipEntity().fromJson(e)).toList() as M;
    }
    throw Exception("not fond");
  }

  static M fromJsonAsT<M>(json) {
    if (json is List) {
      return _getListChildType<M>(json);
    } else {
      return _fromJsonSingle<M>(json) as M;
    }
  }
}
